package planit.controller.impl;

import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;

import planit.model.Model;
import planit.model.Person;
import planit.view.AddPersonToGroupDialog;

@SuppressWarnings("serial")
public class AddPersonToGroupController extends AbstractAction {

	private Model model;

	public AddPersonToGroupController(Model model) {
		this.model = model;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		AddPersonToGroupDialog dialog = (AddPersonToGroupDialog) this
				.getValue("AddPersonToGroupDialog");
		boolean exists = true;
		int gId = dialog.getGroupId();
		int pId = dialog.getPersonId();
		ArrayList<Person> persons = model.getSchedule().getGroup(gId)
				.getPersons();
		for (Person p : persons) {
			if (p == model.getSchedule().getPerson(pId)) {
				exists = false;
			}
		}
		if (exists) {
			model.getSchedule().getGroup(gId).addPerson(
					model.getSchedule().getPerson(pId));
		}
		dialog.updateGroupPersonsList();
	}

}
